export class Tools {
    static smoothPoints(plist) {
        if (plist.length < 2) return; // Not enough points to smooth
        const smoothedPoints = [];
        smoothedPoints.push(plist[0]); // Always include the first point
        for (let i = 1; i < plist.length - 1; i++) {
            const prevPoint = plist[i - 1];
            const currPoint = plist[i];
            const nextPoint = plist[i + 1];
            const smoothedPoint = {
                x: (prevPoint.x + currPoint.x + nextPoint.x) / 3,
                y: (prevPoint.y + currPoint.y + nextPoint.y) / 3
            };
            smoothedPoints.push(smoothedPoint);
        }
        smoothedPoints.push(plist[plist.length - 1]); // Always include the last point
        return smoothedPoints;
    }
    static hitTest(m1, m2) {
        if (m1 === m2) return false;
        const dx = m1.x - m2.x;
        const dy = m1.y - m2.y;
        const distance = Math.sqrt(dx * dx + dy * dy);
        return distance < m1.r + m2.r;
    }
    static hitTestAll(m, list) {
        const collisions = [];
        list.forEach(item => {
            if (Tools.hitTest(m, item)) {
                collisions.push(item);
            }
        });
        return collisions;
    }
    
    static setTimeoutRAF(callback, delay) {
        let start = performance.now();
        const loop = (now) => {
            if (now - start >= delay) {
                callback();
            } else {
                requestAnimationFrame(loop);
            }
        };
        requestAnimationFrame(loop);
    }

}